package com.siwei.mes.service.order.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.order.OrderExperiment;
import com.siwei.mes.mapper.order.OrderExperimentMapper;
import com.siwei.mes.service.order.OrderExperimentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 工单和试验台账的关系(OrderExperiment)表服务实现类
 *
 * @author linzi
 * @since 2024-03-18 17:45:49
 */
@Service
public class OrderExperimentServiceImpl implements OrderExperimentService {
    @Resource
    private OrderExperimentMapper orderExperimentMapper;

    @Override
    public Long create(OrderExperiment entity) {
        orderExperimentMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(OrderExperiment entity) {
        OrderExperiment orderExperiment = orderExperimentMapper.selectByPrimaryKey(entity.getId());
        assertTrue(orderExperiment == null, "工单和试验台账的关系信息不存在");
        orderExperimentMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        OrderExperiment orderExperiment = orderExperimentMapper.selectByPrimaryKey(id);
        assertTrue(orderExperiment == null, "工单和试验台账的关系信息不存在");
        orderExperimentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<OrderExperiment> getPage(PageQuery<OrderExperiment, OrderExperiment> query) {
        OrderExperiment searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> orderExperimentMapper.getList(searchParam));
    }

    @Override
    public OrderExperiment loadById(Long id) {
        return orderExperimentMapper.loadById(id);
    }

    @Override
    public List<OrderExperiment> getAll(OrderExperiment entity) {
        return orderExperimentMapper.getList(entity);
    }
}
